package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetExchange;
import com.example.yiwei.crm.entity.ExchangeEntity;
import com.example.yiwei.crm.json.JLJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.Class.listviewClass.ExchangeAdapter;
import com.example.yiwei.crm.view.activity.coordinationActivity.DetaileJLActivity;
import com.example.yiwei.crm.view.activity.coordinationActivity.SeacherJLActivity;
import com.example.yiwei.crm.view.activity.coordinationActivity.SimpleCoorCustomerActivity;
import com.example.yiwei.crm.view.activity.coordinationActivity.SimpleJLActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class JYCooredJLActivity extends BaseActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{
    private ExchangeAdapter adapter;
    private int dock_id;
    private AutoListView listView;
    private List<ExchangeEntity> list ;
    private List<ExchangeEntity> alist;
    private SharedPreferences sp;
    private int page=1;
    public static SimpleJLActivity b;
    private ImageView lcIcon1,lcIcon2;
    private List<Integer> id_list = new ArrayList<>();
    ImageView fabIconStar;
    FloatingActionMenu leftCenterMenu;
    FloatingActionButton leftCenterButton;
    private String KJA02_B,KJA02_E;
    int a=1;
    ImageView img_tj,img_find,img_lr,img_me,img_back;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<ExchangeEntity> result = (List<ExchangeEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    listView.onRefreshComplete();
                    if (list!=null){
                        list.clear();
                    }
                    if (list!=null){
                        list.addAll(result);
                    }else {
                        list = result;
                    }
                    if (a==1){
                        adapter = new ExchangeAdapter(JYCooredJLActivity.this,list);
                        listView.setAdapter(adapter);
                        a++;
                    }

                    break;
                case AutoListView.LOAD:
                    listView.onLoadComplete();
                    if (result!=null){
                        list.addAll(result);
                    }

                    break;
            }
            if (result!=null) {
//                listView.setResultSize(result.size());
            }else{
//                listView.setResultSize(0);
            }
            adapter.notifyDataSetChanged();

        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jycoored_jl);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        img_back= (ImageView) findViewById(R.id.detailed_doc_img_back);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_lr= (ImageView) findViewById(R.id.item_image_lr);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        img_find= (ImageView) findViewById(R.id.item_image_cx);
        dock_id = getIntent().getIntExtra("dockid",0);
        KJA02_B = getIntent().getStringExtra("KJA02_B");
        KJA02_E = getIntent().getStringExtra("KJA02_E");
        listView = (AutoListView) findViewById(R.id.simple_jl_listview);
        listView.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        listView.setDividerHeight(1);
        listView.setOnRefreshListener(this);
        listView.setOnLoadListener(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
//                    object.put("KXA01",dock_id);
                    object.put("KJA02_B",KJA02_B);
                    object.put("KJA02_E",KJA02_E);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json =object.toString();
                String result = NetWorkUtils.getCount(baseClass.find_JL_msg_URL + 1, json);
                list = JLJson.get(result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (list!=null){
                            adapter = new ExchangeAdapter(JYCooredJLActivity.this,list);
                            listView.setAdapter(adapter);


                        }else {
                            Toast.makeText(JYCooredJLActivity.this, "该条件下未搜素到信息", Toast.LENGTH_SHORT).show();
                        }
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(JYCooredJLActivity.this,JYDetailedJLActivity.class);
                                GetExchange.creat();
                                GetExchange.exchangeEntity = list.get(position-1);
                                startActivity(intent);
                            }
                        });
                    }
                });
            }
        }).start();
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_lr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCooredJLActivity.this, JDcoorActivity.class);
                startActivity(intent);
                finish();

            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCooredJLActivity.this, JDcoorActivity.class);
                intent.putExtra("tt","tt");
                startActivity(intent);
                finish();
            }
        });
        img_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCooredJLActivity.this, JDcoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCooredJLActivity.this, JDcoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            page=page+1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
//                object.put("KXA01",dock_id);
                object.put("KJA02_B",KJA02_B);
                object.put("KJA02_E",KJA02_E);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json =object.toString();
            String result = NetWorkUtils.getCount(baseClass.find_JL_msg_URL + page, json);
            alist = JLJson.get(result);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
            if (alist!=null){
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                msg.obj = alist;
                handler.sendMessage(msg);
                Toast.makeText(JYCooredJLActivity.this, "无更多数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            page =1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
                object.put("KJA02_B",KJA02_B);
                object.put("KJA02_E",KJA02_E);
//                object.put("KXA01",dock_id);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json =object.toString();
            String result = NetWorkUtils.getCount(baseClass.find_JL_msg_URL + 1, json);
            Log.d("ccc",result);
            alist = JLJson.get(result);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (alist!=null){
                listView.setVisibility(View.VISIBLE);
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                listView.setVisibility(View.GONE);
                Toast.makeText(JYCooredJLActivity.this, "无数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
           UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
           LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }

    }
    @Override
    protected void onRestart() {
        super.onRestart();
        onRefresh();
    }
}
